package com.shujia.spark.core

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

object Demo10Sort {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf()
    conf.setMaster("local")
    conf.setAppName("Demo9SumScore")

    val sc = new SparkContext(conf)

    val scoresRDD: RDD[String] = sc.textFile("data/score.txt")

    val scoreKVRDD: RDD[(String, Double)] = scoresRDD
      .map(_.split(","))
      .map { case Array(id, _, score) => (id, score.toDouble) }

    //计算总分
    val sumScoreRDD: RDD[(String, Double)] = scoreKVRDD.reduceByKey(_ + _)


    //sortBy:排序
    //算子效率顺序，sortBy--distinct->join->groupBy->reduceByKey->map.flatMap,filter
    val sortByRDD: RDD[(String, Double)] = sumScoreRDD
      .sortBy(kv => kv._2, ascending = false)

    sortByRDD.foreach(println)
  }
}
